package handler

import (
	"net/http"
	// "strings"

	log "gitee.com/kobeyyf/apibox/logger"
	"github.com/spf13/viper"
)

var (
	logger *log.Logger
)

const (
	ACTION_NAME = "Action"
	APPCODE     = "AppCode"
)

var (
	RewriteHttpCode = true
)

func SetLogger(outlogger *log.Logger) {
	logger = outlogger
}

type Context struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
	Config         *viper.Viper

	isParseForm bool
}

type HandlerFunc func(c *Context) *Response

func (c *Context) Parse() error {
	if c.isParseForm {
		return nil
	}

	// cType := c.Request.Header.Get("Content-Type")
	// if strings.Contains(cType, "application/x-www-form-urlencoded") {
	if err := c.Request.ParseForm(); err != nil {
		return err
	}
	// }
	// if strings.HasPrefix(cType, "multipart/form-data") {
	// 	if err := c.Request.ParseMultipartForm(1024 * 1024 * 1024); err != nil {
	// 		return err
	// 	}
	// }
	c.isParseForm = true
	return nil
}
